﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WhsFacebook.Net.Web.Database;

namespace WhsFacebook.Net.Web.Facebook.Handlers
{
    /// <summary>
    /// Summary description for PostRemove
    /// 
    /// see http://wiki.developers.facebook.com/index.php/Post-Remove_Callback_URL
    /// see 
    /// </summary>
    public class PostRemove : SafeHttpHandler
    {

        protected override void ProcessRequestSafe(HttpContext context)
        {
            if (string.IsNullOrEmpty(FacebookConverter.ToString(context.Request.Params["fb_sig_time"])))
                throw new InvalidOperationException("This URL is to be called from Facebook only!");

            // removing?
            if (!Convert.ToBoolean(context.Request.Params["fb_sig_uninstall"]))
                return;

            // gather all data
            var ts = FacebookConverter.FromUnixTime(context.Request.Params["fb_sig_time"]);
            var uid = FacebookConverter.ToInt64(context.Request.Params["fb_sig_user"]);

            // log
            using (var e = new WhsFacebookEntities())
            {
                var ua = new DbUserRemoveLog()
                {
                    FacebookUserId = uid,
                    IsLoggedIn = true,
                    Occurrence = ts,
                };

                e.AddToUserActionsLog(ua);

                e.SaveChanges();
            }
        }

    }
}